package eteam.aps.service.impl;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.DataDictionaryCategoryMapper;
import eteam.aps.model.entity.DataDictionaryCategory;
import eteam.aps.model.vo.*;
import eteam.aps.service.DataDictionaryCategoryService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 字典分类业务逻辑层
 *
 * @author yiyikai
 */
@Service
public class DataDictionaryCategoryServiceImpl implements DataDictionaryCategoryService {

	@Autowired
	private DataDictionaryCategoryMapper dictionaryCategoryMapper;

	@Autowired
	private LoginUserRedisService loginUserRedisService;

	@Transactional
	@Override
	public int insert(DataDictionaryCategory category) throws Exception{
		VerifyTokenDto user = loginUserRedisService.getUser();
		category.setfId(Helper.GetId());
		category.setfState(ParentEntity.FSTATE_DRAFT);
		category.setfCreateUserId(user.getFuserid());
		category.setfBookCode(user.getFgrouporgid());
		category.setfDdcManagerId(user.getFuserid());
		LocalDateTime ldt = LocalDateTime.now();
		category.setfCreatorTime(ldt);
		category.setfLastModifyTime(ldt);
		return dictionaryCategoryMapper.insertOne(category);
	}

	@Transactional
	@Override
	public int delete(String fId){
		return dictionaryCategoryMapper.delete(fId);
	}

	@Transactional
	@Override
	public int update(DataDictionaryCategory category) throws Exception {
		VerifyTokenDto user = loginUserRedisService.getUser();
		category.setfLastModifyUserId(user.getFuserid());
		category.setfLastModifyTime(LocalDateTime.now());
		return dictionaryCategoryMapper.updateBasicsByFId(category);
	}

	@Override
	public DataDictionaryCategoryDetailsVo selectByFId(String fId) {
		return dictionaryCategoryMapper.selectByFId(fId);
	}

	@Override
	public DataDictionaryCategoryUpdateVo selectUpdate(String fId) {
		return dictionaryCategoryMapper.selectUpdate(fId);
	}

	@Override
	public JsonPage<DataDictionaryCategoryBasicsVo> selectAll(Map<String, Object> map, int page, int pageSize) {
		map.put("fState",ParentEntity.FSTATE_DELETED);
		int totalCount = dictionaryCategoryMapper.selectCountAll(map);
		List<DataDictionaryCategoryBasicsVo> list = null;
		if (totalCount > 0) {
			map.put("offset", (page - 1) * pageSize);
			map.put("limit", pageSize);
			list = dictionaryCategoryMapper.selectAll(map);
		}
		return JsonPage.builder(page, pageSize, totalCount, list);
	}

	@Override
	public List<DataDictionaryCategoryInquireVo> selectAllInquire(Map<String, Object> map) {
		map.put("fState", ParentEntity.FSTATE_DELETED);
		return dictionaryCategoryMapper.selectAllInquire(map);
	}

	@Override
	public int selectCountByFId(String fId) {
		return dictionaryCategoryMapper.selectCountByFId(fId);
	}

	@Override
	public int selectCountByFDdcCode(String fDdcCode) {
		return dictionaryCategoryMapper.selectCountByFDdcCode(fDdcCode);
	}

	@Override
	public int selectCountByFDdcName(String fDdcName) {
		return dictionaryCategoryMapper.selectCountByFDdcName(fDdcName);
	}

	@Override
	public int selectByFState(String fId) {
		return dictionaryCategoryMapper.selectByFState(fId);
	}

	@Override
	public List<DataDictionaryCategoryTreeVo> selectFDdcTree() {
		return dictionaryCategoryMapper.selectFDdcTree(ParentEntity.FSTATE_DELETED);
	}
}